package com.mq.listener;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.json.JSONObject;
import org.mockito.internal.stubbing.BaseStubbing;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Component;

@Component
public class SpringAmqpListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String message) {
//        System.out.println("已收到来自simple.queue的消息：【" + message + "】");
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue1(String message) throws InterruptedException {
        System.out.println("消费者1---收到来自simple.queue的消息：【" + message + "】");
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue2(String message) throws InterruptedException {
        System.err.println("消费者2...收到来自simple.queue的消息：【" + message + "】");
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        System.out.println("消费者收到来自fanout.queue1的消息：【" + message + "】");
        Thread.sleep(20);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        System.out.println("消费者收到来自fanout.queue2的消息：【" + message + "】");
        Thread.sleep(200);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"), // 声明队列
            exchange = @Exchange(name = "zhl.direct", type = ExchangeTypes.DIRECT), // 交换机类型，默认为direct
            key = {"front","all"} // 为队列绑定key
    ))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者收到来自direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"), // 声明队列
            exchange = @Exchange(name = "zhl.direct", type = ExchangeTypes.DIRECT), // 交换机类型，默认为direct
            key = {"back","all"} // 为队列绑定key
    ))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者收到来自direct.queue2的消息：【" + msg + "】");
        User user = JSON.parseObject(msg, User.class);
        System.out.println(user.getName());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "zhl.topic", type = ExchangeTypes.TOPIC),
            key = "liaoning.#"
    ))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者收到来自topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "zhl.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者收到来自topic.queue2的消息：【" + msg + "】");
    }
}

@Data
class User {
    private Integer id;
    private String name;
}
